package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.mvp.presenter.RegisterPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.utils.AppManager;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lihao on 2018/1/11.
 */

public class RegisterActivity extends BaseActivity implements Contract.RegisterView {

    RegisterPresenter registerPresenter = new RegisterPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.tv_getcode)
    TextView tvGetcode;
    @Bind(R.id.et_code)
    EditText etCode;
    @Bind(R.id.et_nike_name)
    EditText etNikeName;
    @Bind(R.id.et_password)
    EditText etPassword;
    @Bind(R.id.et_password1)
    EditText etPassword1;
    @Bind(R.id.btn_regsiter)
    Button btnRegsiter;
    @Bind(R.id.et_tuijian_code)
    EditText etTuijianCode;
    @Bind(R.id.m_checkbox)
    CheckBox mCheckbox;
    @Bind(R.id.tv_agreement)
    TextView tvAgreement;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {
    }

    protected void initView() {
        tvTitle.setText("注册");
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_back, R.id.tv_getcode, R.id.btn_regsiter, R.id.tv_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_getcode:
                registerPresenter.getCode(etPhone.getText().toString());
                break;
            case R.id.btn_regsiter:
                if (mCheckbox.isChecked()){
                    registerPresenter.register(etPhone.getText().toString().trim(), etNikeName.getText().toString().trim(), etCode.getText().toString(), etPassword.getText().toString(), etPassword1.getText().toString(), etTuijianCode.getText().toString());
                }else {
                    showToast("请阅读并同意《优品日记用户协议》");
                }
                break;
            case R.id.tv_agreement:
                intent = new Intent(BaseActivity.context, WebViewActivity.class);
                intent.putExtra("url", URLS.HOST+URLS.REGISTRATION);
                intent.putExtra("title","注册协议");
                startActivity(intent);

                break;
        }
    }


    @Override
    public void CountDownTimerFinish() {
        tvGetcode.setTextColor(getResources().getColor(R.color.theme_color));
        tvGetcode.setText("获取验证码");
        tvGetcode.setEnabled(true);
    }

    @Override
    public void CountDownTimeronTick(long millisUntilFinished) {
        tvGetcode.setTextColor(getResources().getColor(R.color.text_color_gray));
        tvGetcode.setText("请等待" + millisUntilFinished / 1000 + "秒");
        tvGetcode.setEnabled(false);
    }

    @Override
    public void registerSuccess() {
        AppManager.getAppManager().finishActivity();
    }

}
